#include <iostream>

using namespace std;
const int N=3*1e4+10;

int d[N];
int f[N];
int s[N];
int t;
int find(int x)
{
	if(f[x]!=x)
	{
		int root=find(f[x]);
		d[x]+=d[f[x]];
		f[x]=root;
	}
	return f[x];
}
int main()
{
	cin>>t;
	for(int i=1;i<N;i++)
	{
		f[i]=i;
		s[i]=1;
	}
	while(t--)
	{
		char op[2];
		int x,y;
		scanf("%s%d%d",op,&x,&y);
		if(*op=='M')
		{
			int tx=find(x),ty=find(y);
			if(tx!=ty)
			{
				d[tx]=s[ty];
				s[ty]+=s[tx];
				f[tx]=ty;
			}
		}
		else{
			int tx=find(x),ty=find(y);
			if(tx!=ty)
				cout<<-1<<endl;
			else
				cout<<max(0,abs(d[x]-d[y])-1)<<endl;
		}
	}
	return 0;
}
